import KoaRouter from 'koa-router'
import Config from '@/config/Config'
import verifyToken from '@/middlewares/verifyToken'
import { Models } from '@/common/typings/modle'
import { Success } from '@/core/HttpException'
import { getRoleList } from '@/api/v1/system/role/list'
const router = new KoaRouter({
  prefix: `${Config.API_PREFIX}v1/system/resource`,
})
router.post('/getListByUserId', verifyToken, async (ctx: Models.Ctx) => {
  const { id } = ctx.request.body as any
  const list = await getResourceListByUserId(id)
  throw new Success({
    data: list,
  })
})
export default router

export async function getResourceListByUserId(id: any) {
  const { finallyResult } = await getRoleList({ userList: [id] })
  const list: any[] = []
  finallyResult.forEach((result: any) => {
    if (result.resourceList && result.resourceList.length) {
      result.resourceList.forEach((rItem: any) => {
        if (!list.some((lItem) => lItem.id === rItem.id)) {
          list.push(rItem)
        }
      })
    }
  })
  return list
}
